﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SQLite;
using System.Drawing.Printing;
namespace doan_pdf_txt_reader
{
    public partial class Form1 : Form
    {
        DirectoryInfo database_dir = new DirectoryInfo(Directory.GetCurrentDirectory() + "/data/");
        DirectoryInfo cur_dir = new DirectoryInfo(Directory.GetCurrentDirectory() + "/data/");
        public string new_folder_name = "";
        public Form1()
        {
            InitializeComponent();

            open_directory();

        }

        private void btn_new_folder_Click(object sender, EventArgs e)
        {
            new_folder_form frm_new_folder = new new_folder_form();
            frm_new_folder.frm1 = this;
            frm_new_folder.ShowDialog();
            if (tvw_database.SelectedNode != null)
            {
                TreeNode selected_node = tvw_database.SelectedNode;
                if (selected_node.Tag.GetType() == typeof(DirectoryInfo))
                {
                    selected_node.Nodes.Clear();
                    DirectoryInfo dir = (DirectoryInfo)selected_node.Tag;
                    if (new_folder_name != "")
                    {
                        cur_dir = Directory.CreateDirectory(cur_dir.FullName + "/" + new_folder_name);
                        foreach (DirectoryInfo sub_dir in dir.GetDirectories())
                        {
                            TreeNode dir_node = new TreeNode(sub_dir.Name);
                            dir_node.Tag = sub_dir;
                            selected_node.Nodes.Add(dir_node);
                        }
                        foreach (FileInfo file in dir.GetFiles())
                        {
                            TreeNode file_node = new TreeNode(file.Name);
                            file_node.Tag = file;
                            selected_node.Nodes.Add(file_node);
                        }
                    }
                }
                selected_node.Expand();
            }
            else
            {
                cur_dir = Directory.CreateDirectory(cur_dir.FullName + "/" + new_folder_name);
            }
            string note_dir = cur_dir.FullName.Replace("data", "note");
            Directory.CreateDirectory(note_dir);
            open_directory();
            new_folder_name = "";
        }

        private void btun_remove_Click(object sender, EventArgs e)
        {
            if (tvw_database.SelectedNode.Tag.GetType() == typeof(DirectoryInfo))
            {
                string old_dir = cur_dir.Parent.FullName;
                cur_dir = (DirectoryInfo)tvw_database.SelectedNode.Tag;
                RecursiveDelete(old_dir, cur_dir.Name);
            }
            else
            {
                FileInfo file = (FileInfo)tvw_database.SelectedNode.Tag;
                File.Delete(file.FullName);
                File.Delete(file.FullName.Replace("data", "note").Replace(".pdf", ".txt")); // !!!
            }
            cur_dir = database_dir;
            open_directory();
        }
        private void open_directory()
        {
            tvw_database.Nodes.Clear();
            foreach (DirectoryInfo dir in database_dir.GetDirectories())
            {
                TreeNode directory_node = new TreeNode(dir.Name);
                directory_node.Tag = dir;
                tvw_database.Nodes.Add(directory_node);
            }
            foreach (FileInfo file in database_dir.GetFiles())
            {
                TreeNode file_node = new TreeNode(file.Name);
                file_node.Tag = file;
                tvw_database.Nodes.Add(file_node);
            }
        }

        private void RecursiveDelete(string path, string name)
        {
            foreach (string directory in Directory.GetDirectories(path))
            {
                if (directory.EndsWith("\\" + name))
                {
                    Directory.Delete(directory, true);
                    Directory.Delete(directory.Replace("data", "note"), true);
                }
                else
                {
                    RecursiveDelete(directory, name);
                    RecursiveDelete(directory.Replace("data", "note"), name);
                }
            }
        }
        private void recursive_find(string path, string name)
        {
            tvw_database.Nodes.Clear();
            foreach (string directory in Directory.GetDirectories(path, "*" + tb_search_keyword.Text + "*", SearchOption.AllDirectories))
            {
                TreeNode directory_node = new TreeNode(directory);
                directory_node.Tag = new DirectoryInfo(directory);
                tvw_database.Nodes.Add(directory_node);
            }
            foreach (string file in Directory.GetFiles(path, "*" + tb_search_keyword.Text + "*", SearchOption.AllDirectories))
            {
                TreeNode file_node = new TreeNode(file);
                file_node.Tag = new FileInfo(file);
                tvw_database.Nodes.Add(file_node);
            }
        }


        private void btn_search_Click(object sender, EventArgs e)
        {
            stt_searching.Visible = true;
            recursive_find(database_dir.FullName, tb_search_keyword.Text);
            stt_searching.Visible = false;
        }

        private void btn_add_to_collection_Click(object sender, EventArgs e)
        {
            stt_add_to_collection.Visible = true;
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "All file (*.*)|*.*";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                File.Copy(dialog.FileName, cur_dir.FullName + "/" + dialog.SafeFileName, true);
                string file_dir = cur_dir.FullName + "/" + dialog.SafeFileName;
                string note_dir = file_dir.Replace("data", "note"); // !!!
                FileStream fs = File.Create(note_dir.Replace(".pdf", ".txt")); // !!!
                fs.Close();
            }
            open_directory();
            stt_add_to_collection.Visible = false;
        }

        private void tvw_database_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode selected_node = tvw_database.SelectedNode;
            if (selected_node.Tag.GetType() == typeof(DirectoryInfo))
            {
                selected_node.Nodes.Clear();
                DirectoryInfo dir = (DirectoryInfo)selected_node.Tag;
                foreach (DirectoryInfo sub_dir in dir.GetDirectories())
                {
                    TreeNode dir_node = new TreeNode(sub_dir.Name);
                    dir_node.Tag = sub_dir;
                    selected_node.Nodes.Add(dir_node);
                }
                foreach (FileInfo sub_file in dir.GetFiles())
                {
                    TreeNode file_node = new TreeNode(sub_file.Name);
                    file_node.Tag = sub_file;
                    selected_node.Nodes.Add(file_node);
                }
                cur_dir = dir;
                selected_node.Expand();
                stt_file_name.Text = "Folder: " + dir.Name;
            }
            else
            {
                FileInfo selected_file = (FileInfo)selected_node.Tag;
                if (selected_file.FullName.EndsWith("pdf"))
                {
                    com_display_pdf.Visible = true;
                    tb_display_document.Visible = false;
                    com_display_pdf.LoadFile(selected_file.FullName);
                }
                else if (selected_file.FullName.EndsWith("txt"))
                {
                    btn_print_txt.Visible = true;
                    com_display_pdf.Visible = false;
                    FileStream theFile = File.Open(selected_file.FullName, FileMode.Open, FileAccess.Read);
                    StreamReader reader = new StreamReader(theFile);

                    tb_display_document.Text = reader.ReadToEnd();
                    reader.Close();
                }
                stt_file_name.Text = "File: " + selected_file.Name;
                open_note(selected_file);
            }
        }
        private void PrintDocumentOnPrintPage(object sender, PrintPageEventArgs e)
        {
            e.Graphics.DrawString(this.tb_display_document.Text, this.tb_display_document.Font, Brushes.Black, 10, 25);
        }

        private void btn_print_txt_Click(object sender, EventArgs e)
        {
            stt_printing.Visible = true;
            PrintDocument printDocument = new PrintDocument();
            printDocument.PrintPage += PrintDocumentOnPrintPage;
            printDocument.Print();
            stt_printing.Visible = false;
        }

        public void open_note(FileInfo document_object)
        {
            string document_dir = document_object.DirectoryName;
            string note_dir = document_dir.Replace("data", "note"); // !!!
            FileStream theFile = File.Open(note_dir + "/" + document_object.Name.Replace(".pdf", ".txt"), FileMode.Open, FileAccess.Read);
            StreamReader reader = new StreamReader(theFile);
            tb_note.Text = reader.ReadToEnd();
            reader.Close();
        }

        private void btn_exit_program_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_save_note_Click(object sender, EventArgs e)
        {
            TreeNode selected_node = tvw_database.SelectedNode;
            FileInfo selected_file = (FileInfo)selected_node.Tag;
            FileStream theFile = File.Open(selected_file.FullName.Replace("data", "note").Replace(".pdf", ".txt"), FileMode.Open, FileAccess.Write);
            StreamWriter writer = new StreamWriter(theFile);
            writer.Write(tb_note.Text);
            writer.Close();
        }
    }
}
